package com.nifostasky.comic.controller;

import com.nifostasky.comic.entitys.SysMenu;
import com.nifostasky.comic.exceptions.BusinessException;
import com.nifostasky.comic.query.SysMenuQuery;
import com.nifostasky.comic.service.SysMenuService;
import com.nifostasky.comic.utils.PageUtils;
import com.nifostasky.comic.vos.JsonResult;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/sysMenu")
@Api(tags = "菜单管理", position = 5)
@AllArgsConstructor
public class SysMenuController {
    private SysMenuService sysMenuService;

    @ApiOperation(value = "菜单列表",notes = "带参分页查询列表")
    @GetMapping("findListPage")
    @ApiOperationSupport(order = 1)
    public JsonResult findListPage(PageUtils pageUtils, SysMenuQuery menuQuery){
        return new JsonResult(sysMenuService.findListPage(pageUtils, menuQuery));
    }

    @ApiOperation("新增/修改")
    @PostMapping("/saveOrUpdate")
    @ApiOperationSupport(order = 2)
    public JsonResult saveOrUpdate(@RequestBody SysMenu sysMenu){
        return new JsonResult(sysMenuService.saveOrUpdateMenu(sysMenu));
    }

    @ApiOperation("根据主键id获取信息")
    @GetMapping("/getData")
    @ApiResponses(@ApiResponse(code = 200,message = "返回成功",response = SysMenu.class))
    @ApiOperationSupport(order = 3)
    public JsonResult<SysMenu> getData(@ApiParam(value = "主键id",required = true) Long id){
        return new JsonResult(sysMenuService.getById(id));
    }
    @ApiOperation("单个或批量删除")
    @GetMapping("/deleteByIds")
    @ApiOperationSupport(order = 4)
    public JsonResult deleteByIds(@ApiParam("主键ids，批量删除时id用英文逗号隔开数据格式:id1,id2,id3,...") String ids){
        if(StringUtils.isBlank(ids)){
            throw new BusinessException("缺少必填参数");
        }
        List<String> listId = Arrays.asList(ids.split(","));
        Boolean bool = sysMenuService.removeByIds(listId);
       return new JsonResult(bool);
    }

    @ApiOperation("修改")
    @GetMapping("/updateMenuState")
    @ApiOperationSupport(order = 4)
    public JsonResult hasEnable(@ApiParam("菜单id") @RequestParam Long id,@ApiParam("状态:1启用，2禁用") @RequestParam Integer state){
        return new JsonResult(sysMenuService.updateMenuState(id,state));
    }
}
